package com.university.evaluation.utils;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.HashMap;
import java.util.Map;

public class ResponseUtils {

    // 成功响应的状态码
    public static final String CODE_SUCCESS = "200";
    // 失败响应的状态码
    public static final String CODE_ERROR = "500";
    // 参数错误的状态码
    public static final String CODE_PARAM_ERROR = "400";
    // 未授权的状态码
    public static final String CODE_UNAUTHORIZED = "401";
    // 禁止访问的状态码
    public static final String CODE_FORBIDDEN = "403";
    // 资源不存在的状态码
    public static final String CODE_NOT_FOUND = "404";

    // 成功消息
    public static final String MSG_SUCCESS = "操作成功";
    // 失败消息
    public static final String MSG_ERROR = "操作失败";
    // 参数错误消息
    public static final String MSG_PARAM_ERROR = "参数错误";
    // 未授权消息
    public static final String MSG_UNAUTHORIZED = "未授权";
    // 禁止访问消息
    public static final String MSG_FORBIDDEN = "禁止访问";
    // 资源不存在消息
    public static final String MSG_NOT_FOUND = "资源不存在";

    // 创建成功响应
    public static <T> ResponseEntity<Map<String, Object>> success() {
        return success(null);
    }

    // 创建带数据的成功响应
    public static <T> ResponseEntity<Map<String, Object>> success(T data) {
        return success(CODE_SUCCESS, MSG_SUCCESS, data);
    }

    // 创建带自定义消息的成功响应
    public static <T> ResponseEntity<Map<String, Object>> success(String message) {
        return success(CODE_SUCCESS, message, null);
    }

    // 创建带自定义消息和数据的成功响应
    public static <T> ResponseEntity<Map<String, Object>> success(String message, T data) {
        return success(CODE_SUCCESS, message, data);
    }

    // 创建完全自定义的成功响应
    public static <T> ResponseEntity<Map<String, Object>> success(String code, String message, T data) {
        Map<String, Object> response = buildResponse(code, message, data);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    // 创建失败响应
    public static ResponseEntity<Map<String, Object>> error() {
        return error(MSG_ERROR);
    }

    // 创建带自定义错误消息的响应
    public static ResponseEntity<Map<String, Object>> error(String message) {
        return error(CODE_ERROR, message);
    }

    // 创建完全自定义的错误响应
    public static ResponseEntity<Map<String, Object>> error(String code, String message) {
        Map<String, Object> response = buildResponse(code, message, null);
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        if (CODE_PARAM_ERROR.equals(code)) {
            status = HttpStatus.BAD_REQUEST;
        } else if (CODE_UNAUTHORIZED.equals(code)) {
            status = HttpStatus.UNAUTHORIZED;
        } else if (CODE_FORBIDDEN.equals(code)) {
            status = HttpStatus.FORBIDDEN;
        } else if (CODE_NOT_FOUND.equals(code)) {
            status = HttpStatus.NOT_FOUND;
        }
        return new ResponseEntity<>(response, status);
    }

    // 构建基础响应对象
    private static <T> Map<String, Object> buildResponse(String code, String message, T data) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", code);
        response.put("message", message);
        if (data != null) {
            response.put("data", data);
        }
        response.put("timestamp", DateUtils.currentTimeSeconds());
        return response;
    }

    // 创建分页响应
    public static <T> ResponseEntity<Map<String, Object>> pageResponse(T records, long total, int page, int pageSize) {
        Map<String, Object> pageData = new HashMap<>();
        pageData.put("records", records);
        pageData.put("total", total);
        pageData.put("page", page);
        pageData.put("pageSize", pageSize);
        pageData.put("pages", total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        return success(pageData);
    }

    // 创建参数错误响应
    public static ResponseEntity<Map<String, Object>> paramError() {
        return error(CODE_PARAM_ERROR, MSG_PARAM_ERROR);
    }

    // 创建带自定义消息的参数错误响应
    public static ResponseEntity<Map<String, Object>> paramError(String message) {
        return error(CODE_PARAM_ERROR, message);
    }

    // 创建未授权响应
    public static ResponseEntity<Map<String, Object>> unauthorized() {
        return error(CODE_UNAUTHORIZED, MSG_UNAUTHORIZED);
    }

    // 创建带自定义消息的未授权响应
    public static ResponseEntity<Map<String, Object>> unauthorized(String message) {
        return error(CODE_UNAUTHORIZED, message);
    }

    // 创建禁止访问响应
    public static ResponseEntity<Map<String, Object>> forbidden() {
        return error(CODE_FORBIDDEN, MSG_FORBIDDEN);
    }

    // 创建带自定义消息的禁止访问响应
    public static ResponseEntity<Map<String, Object>> forbidden(String message) {
        return error(CODE_FORBIDDEN, message);
    }

    // 创建资源不存在响应
    public static ResponseEntity<Map<String, Object>> notFound() {
        return error(CODE_NOT_FOUND, MSG_NOT_FOUND);
    }

    // 创建带自定义消息的资源不存在响应
    public static ResponseEntity<Map<String, Object>> notFound(String message) {
        return error(CODE_NOT_FOUND, message);
    }

    // 判断响应是否成功
    public static boolean isSuccess(ResponseEntity<Map<String, Object>> response) {
        if (response == null || response.getBody() == null) {
            return false;
        }
        return CODE_SUCCESS.equals(response.getBody().get("code"));
    }

    // 从响应中获取数据
    @SuppressWarnings("unchecked")
    public static <T> T getData(ResponseEntity<Map<String, Object>> response, Class<T> clazz) {
        if (response == null || response.getBody() == null || !response.getBody().containsKey("data")) {
            return null;
        }
        Object data = response.getBody().get("data");
        if (data == null) {
            return null;
        }
        if (clazz.isInstance(data)) {
            return (T) data;
        }
        throw new ClassCastException("无法将响应数据转换为指定类型");
    }

    // 从响应中获取消息
    public static String getMessage(ResponseEntity<Map<String, Object>> response) {
        if (response == null || response.getBody() == null || !response.getBody().containsKey("message")) {
            return null;
        }
        return (String) response.getBody().get("message");
    }

    // 从响应中获取状态码
    public static String getCode(ResponseEntity<Map<String, Object>> response) {
        if (response == null || response.getBody() == null || !response.getBody().containsKey("code")) {
            return null;
        }
        return (String) response.getBody().get("code");
    }

}